import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, LoadingController, ViewController, ToastController } from 'ionic-angular';
import { Find2Page } from '../find2/find2';
import { LoginPage } from '../login/login';
import { RegisterPage } from '../register/register';
import { baseCommon } from '../../common/baseCommon';
import {
  Http,
  Headers,
  RequestOptions,
  URLSearchParams
}from '@angular/http';
import { AppConfig } from'./../../app/app.config';
/**
 * Generated class for the FindPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-find',
  templateUrl: 'find.html',
})
export class FindPage{

  phone:any;

  constructor(public navCtrl: NavController,
     public navParams: NavParams,    
    public http: Http,
    public loadingCtrl:LoadingController,
    public viewCtrl:ViewController,
    public toastCtrl:ToastController) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad FindPage');
  }

  /**
   * 下一步
   * 
   * @memberof FindPage
   */
  next(){
    this.getRegisterMessage(AppConfig.httpLoc+'/reset/message',this.phone);
  }

  /**
   * 返回登录
   * 
   * @memberof FindPage
   */
  login(){
    this.navCtrl.push(LoginPage)
  }

  /**
   * 注册
   * 
   * @memberof FindPage
   */
  register(){
    this.navCtrl.push(RegisterPage)
  }

  /**
   * 输入手机号获得验证码
   * 
   * @param {any} apiUrl 
   * @param {any} phone 
   * @memberof FindPage
   */
  getRegisterMessage(apiUrl,phone){
    let headers = new Headers({
      'Content-Type': 'application/x-www-form-urlencoded'
    });

    let options = new RequestOptions({
      headers: headers
    });

    let pramas :URLSearchParams = new URLSearchParams();

    pramas.set('phone',phone);

    let getData:any;

    this.http.post(apiUrl, pramas, options)
      .toPromise()
      .then( data => {
        getData = data.json();
        if(getData.message == "成功"){
          this.navCtrl.push(Find2Page,{'phone':this.phone})
        }else{
          baseCommon.showToast(this.toastCtrl,getData.message);
        }
      }, err => {
        console.log( err );
      })
    }

}
